package com.xl.bigdata.producer;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.Future;

/**
 * @ClassName ProducerAPI
 * @Description TODO
 * @Author xl.gao
 * @Date 2022/3/23 15:17
 * @Version
 */
public class ProducerAPI {

    public static volatile KafkaProducer<String, String> kafkaProducer;

    public static void sendMessages(String bootstrapServers, String topic, String message){

        if(kafkaProducer == null){
            getKafkaProducer(bootstrapServers);
            System.out.println("ProducerAPI强制初期化连接...");
        }

        kafkaProducer.send(new ProducerRecord<>(topic, message));
    }

    public static void sendMessages(String bootstrapServers, String topic, String message, String key){

        if(kafkaProducer == null){
            getKafkaProducer(bootstrapServers);
            System.out.println("ProducerAPI强制初期化连接...");
        }

        kafkaProducer.send(new ProducerRecord<>(topic, key, message));
    }

    public static KafkaProducer<String, String> getKafkaProducer(String bootstrapServers){

        try{
            if(kafkaProducer == null){
                synchronized (ProducerAPI.class){
                    if(kafkaProducer == null){

                        Properties properties = initProperties(bootstrapServers);
                        kafkaProducer = new KafkaProducer<>(properties);

                    } else {
                        System.out.println("ProducerAPI - 重复使用kafkaProducer连接...双重检查最外层...");
                    }
                }
            } else {
                System.out.println("ProducerAPI - 重复使用kafkaProducer连接...双重检查最外层...");
            }
        } catch (Exception e){
            if(kafkaProducer != null){
                kafkaProducer.close();
                System.out.println("ProducerAPI的getKafkaProducer初期化失败...");
                e.printStackTrace();
            }
        }

        return kafkaProducer;
    }

    public static  Properties initProperties(String bootstrapServers){

        Properties properties = new Properties();

        // 连接集群 bootstrap.servers
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"hadoop102:9092,hadoop103:9092");

        // 指定对应的key和value的序列化类型 key.serializer
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());

        // acks 1等待leader -1等待leader+isr
        properties.put(ProducerConfig.ACKS_CONFIG,"1");

        // 重试次数
        properties.put(ProducerConfig.RETRIES_CONFIG,3);

        // linger.ms 延迟发送数据的等待时间
        properties.put(ProducerConfig.LINGER_MS_CONFIG, 50);

        return properties;
    }
}
